﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Framework;
using DataAccess;
using System.Reflection;

namespace DomMgmtSystem.View
{
    public partial class InCheckHS : System.Web.UI.Page
    {
        #region 属性
        /// <summary>
        /// 
        /// </summary>
        private decimal NID
        {
            get
            {
                if (!string.IsNullOrEmpty(Request["NID"]))
                    return Convert.ToDecimal(Request["NID"]);
                return 0;
            }
        }

        /// <summary>
        /// 是否是添加
        /// </summary>
        public bool IsNew
        {
            get
            {
                if (ViewState["IsNew"] != null)
                    return Convert.ToBoolean(ViewState["IsNew"]);
                else
                    return this.GetRequestStr("isnew") != string.Empty;
            }

            set
            {
                ViewState["IsNew"] = value;
            }
        }

        /// <summary>
        /// 是否是浏览
        /// </summary>
        public bool IsView
        {
            get
            {
                return this.GetRequestStr("view") != string.Empty;
            }

        }

        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataBind();
                txtJC_DATE.Text = DateTime.Now.ToString("yyyy-MM-dd");
            }

            if (Request.Params["view"] != null)
                ClientScript.RegisterStartupScript(typeof(Page), "a", "<script>ChangeToLabel();</script>");
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (NID == 0 && TB_CHECK_HS.Exists(txtSQDH.Text.Trim()))
                {
                    this.Alert("摄区编码已存在，请重新输入摄区编码");
                    return;
                }
                Model.TB_CHECK_HS model = new Model.TB_CHECK_HS();
                if (NID != 0)
                    model = DataAccess.TB_CHECK_HS.GetModel(NID);

                model.SQDH = txtSQDH.Text.Trim();
                model.CSQMC = txtCSQMC.Text.Trim();
                model.BQFHYQ = BQFHYQ0.Checked ? 0 : 1;
                model.CPZJBG = CPZJBG0.Checked ? 0 : 1;
                model.FZSY_CLBG = FZSY_CLBG0.Checked ? 0 : 1;
                model.HSFXJL = HSFXJL0.Checked ? 0 : 1;
                model.HSJQPW = HSJQPW0.Checked ? 0 : 1;
                model.HSJSSJS = HSJSSJS0.Checked ? 0 : 1;
                model.HSYJLSJ_CLSJ = HSYJLSJ_CLSJ0.Checked ? 0 : 1;
                model.HSYJSCS = HSYJSCS0.Checked ? 0 : 1;
                model.HSZLSSBG = HSZLSSBG0.Checked ? 0 : 1;
                model.HXXPJHT = HXXPJHT0.Checked ? 0 : 1;
                model.JC_DATE = Convert.ToDateTime(txtJC_DATE.Text);
                model.JCSFTG = JCSFTG0.Checked ? 0 : 1;
                model.JDB = JDB0.Checked ? 0 : 1;
                model.LSH = CommonExecute.GetLSH();
                model.MLGSFHYQ = MLGSFHYQ0.Checked ? 0 : 1;
                model.RKZT = 0;
                model.SJZLFHYQ = SJZLFHYQ0.Checked ? 0 : 1;
                model.SQFWWCQKT = SQFWWCQKT0.Checked ? 0 : 1;
                model.SSPS_XPS_YZ = SSPS_XPS_YZ0.Checked ? 0 : 1;
                model.TSFHHTYQ = TSFHHTYQ0.Checked ? 0 : 1;
                model.XPZXDJHT = XPZXDJHT0.Checked ? 0 : 1;
                model.XPZXDZB = XPZXDZB0.Checked ? 0 : 1;
                model.YPSFKD = YPSFKD0.Checked ? 0 : 1;
                model.YPSL = txtYPSL.Text.ToInt();
                model.ZLYJS = ZLYJS0.Checked ? 0 : 1;
                model.ZLYJS_FS = txtZLYJS_FS.Text.ToInt();
                model.ZLYJS_YZ = ZLYJS_YZ0.Checked ? 0 : 1;
                model.ZZZLYJS_FS = txtZZZLYJS_FS.Text.ToInt();
                model.ZZZLYJS_YW = ZZZLYJS_YW0.Checked ? 0 : 1;
                model.CREATE_DATE = DateTime.Now;
                model.CREATE_USER = Session["userID"].ToString();

                if (NID != 0)
                {
                    if (TB_CHECK_HS.Update(model))
                        this.RedirectAfterInfo("修改成功！", "InCheckListHS.aspx");
                    else
                        this.Alert("修改失败！");
                }
                else
                    if (TB_CHECK_HS.Add(model))
                    {
                        this.RedirectConfirmInfo("新增成功！是否继续新增？", "InCheckHS.aspx", "InCheckListHS.aspx");
                    }
                    else
                    {
                        this.Alert("保存失败！");
                    }
            }
            catch (Exception ex)
            {
                LogHelper.ErrorFormat("入库检查保存异常:{0}{1}", ex.Message, ex.StackTrace);
                this.Alert("保存异常！");
            }
        }

        public void DataBind()
        {
            if (NID != 0)
            {
                Model.TB_CHECK_HS model = DataAccess.TB_CHECK_HS.GetModel(NID);
                if (model != null)
                {
                    txtSQDH.Text = model.SQDH.ToString();
                    txtCSQMC.Text = model.CSQMC;
                    txtJC_DATE.Text = model.JC_DATE.Value.ToString("yyyy-MM-dd");
                    DivJCDate.Visible = false;
                    lblJCDate.Visible = true;
                    lblJCDate.Text = model.JC_DATE.Value.ToString("yyyy-MM-dd");
                    txtLSH.Text = model.LSH.ToString();
                    txtYPSL.Text = model.YPSL.ToString();
                    txtZLYJS_FS.Text = model.ZLYJS_FS.ToString();
                    txtZZZLYJS_FS.Text = model.ZZZLYJS_FS.ToString();

                    Type type = typeof(Model.TB_CHECK_HS);
                    PropertyInfo[] myFields = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
                    myFields.ForEach(p =>
                    {
                        Control ctrl = Page.FindControlEx(p.Name + "0");
                        if (ctrl != null)
                        {
                            RadioButton rb0 = ctrl as RadioButton;
                            rb0.Checked = p.GetValue(model, null).ToString() == "0";
                            ctrl = Page.FindControlEx(p.Name + "1");
                            RadioButton rb1 = ctrl as RadioButton;
                            rb1.Checked = !rb0.Checked;
                        }
                    });
                }
                btnBack.Visible = true;
                //if (Request.Params["view"] != null)
                //    lbTitle.Text = "查看入库检查";
                //else
                //    lbTitle.Text = "编辑入库检查";
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("InCheckListHS.aspx");
        }
    }
}